home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 007 / simcode.arc / XTRANS.C < prev    next >
Text File  |  1984-11-29  |  5KB  |  241 lines

  1. char soh = 0x01;
  2. char eot = 0x04;
  3. char ack = 0x06;
  4. char nak = 0x15;
  5. char can = 0x18;
  6. #include "stdio.h"
  7. #include "signal.h"
  8. #include "termio.h"
  9. #include "sys/ioctl.h"
  10. #include "sys/types.h"
  11. #include "sys/stat.h"
  12. struct termio save;
  13. struct termio new;
  14. struct stat statbuf;
  15.  
  16. int lfproc = 0;
  17. int debug = 0;
  18. int remote = 0;
  19. FILE *errors;
  20. main(argc, argv)
  21. int argc;
  22. char **argv;
  23. {
  24.     int i,j;
  25.     char buf[130];
  26.     char blocknum = 1;
  27.     char c_blocknum = 254;
  28.     int numread;
  29.     char cksum;
  30.     char inch;
  31.     int alarmfn();
  32.     int kleenex();
  33.     FILE *fp;
  34.     extern char *optarg;
  35.     extern int optind;
  36.     char file[128];
  37.     char remote_name[70];
  38.  
  39.     while ((i = getopt(argc, argv, "rtld")) != EOF)
  40.         switch(i) {
  41.         case 'd':
  42.             debug++;
  43.             break;
  44.         case 'l':
  45.         case 't':    /* TEXT files */
  46.             lfproc++;
  47.             break;
  48.         case 'r':    /* remote file transfer */
  49.             remote++;
  50.             break;
  51.         case '?':
  52.             fprintf(stderr, "Usage: %s [-l] file\n", argv[0]);
  53.             kleenex(0);
  54.             break;
  55.         }
  56.     ioctl(1, TCGETA, &save);
  57.     ioctl(1, TCGETA, &new);
  58.     if (debug) errors = fopen("xt.err", "w");
  59.     if (debug && errors == NULL) {
  60.         fprintf(stderr, "Unable to open xt.err for error tracing\n");
  61.         kleenex(1);
  62.     }
  63.     if (debug) setbuf(errors, NULL);
  64.     if (argc == 1 || argc == optind) {
  65.         fprintf(stderr, "Need file name\n");
  66.         kleenex(0);
  67.     }
  68.     strcpy(file, argv[optind]);
  69.     for (i=1; i<20;i++) signal(i, SIG_IGN);
  70.     signal(14, alarmfn);
  71.     signal(3, kleenex);
  72.     signal(1, kleenex);
  73.     if ((fp=fopen(file, "r")) == NULL) {
  74.         fprintf(stderr, "File not found: %s\n", file);
  75.         kleenex(0);
  76.     }
  77.     stat(file, &statbuf);
  78.     printf("File '%s' ready for transmission.......", file);
  79.     printf("%d blocks\n", statbuf.st_size / 128 + 1);
  80.     printf("Type ^X to exit, xtrans ignores signals\n");
  81.     fflush(stdin);
  82.     new.c_iflag = 0;
  83.     new.c_oflag = 0;
  84.     new.c_lflag &= ~(ICANON|ECHO);
  85.     new.c_cc[4] = 1;
  86.     new.c_cflag &= ~PARENB;
  87.     new.c_cflag |= CS8;
  88.     ioctl(1, TCSETAW, &new);
  89.     if (remote==1){
  90.         int i;
  91.         sprintf(remote_name,"\033dx%s\032",argv[optind+1]);
  92.         for (i=0;remote_name[i];i++)
  93.             if (remote_name[i]=='/') remote_name[i] = '\\';
  94.         write(1,remote_name,strlen(remote_name));
  95.         }
  96.     get_x_char(60, &buf[0]);
  97.     if (buf[0] != nak) {
  98.     if (debug)     fprintf(errors, "Didn't get a startup NAK\n");
  99.         kleenex(0);
  100.     }
  101.     numread = read_in(fp, buf, 128);
  102.     while (numread > 0) {
  103.         if (numread < 128) {
  104.             for (i=numread; i< 128; i++) buf[i] = 0;
  105.         }
  106.         write(1, &soh, 1);
  107.         write(1, &blocknum, 1);
  108.         write(1, &c_blocknum, 1);
  109.         write(1, buf, 128);
  110.         cksum = 0;
  111.         for (i=0;i<128;i++) cksum += buf[i];
  112.         cksum = cksum % 256;
  113.         write(1, &cksum, 1);
  114.         j = get_x_char(15, &inch);
  115.         if (j >= 0 && inch == nak) {
  116.             if (debug) fprintf(errors, "Got a nak after block %d\n",blocknum);
  117.             clear_iq();
  118.             continue;
  119.         }
  120.         if (j >= 0 && inch == can) {
  121.             if (debug) fprintf(errors, "Got a cancel after block %d\n",blocknum);
  122.             kleenex(0);
  123.         }
  124.         if (j >= 0 && inch != ack) {
  125.             if (debug) fprintf(errors, "Got a non-ack after block %d, char was %o\n",blocknum,inch);
  126.             clear_iq();
  127.             continue;
  128.         }
  129.         if (j == -1) {
  130.             if (debug) fprintf(errors, "Got a timeout after block %d\n",blocknum);
  131.             continue;
  132.         }
  133.         numread = read_in(fp, buf, 128);
  134.         blocknum = (blocknum + 1) % 256;
  135.         c_blocknum = 255 - blocknum;
  136.     }
  137.     while (1) {
  138.         write(1, &eot, 1);
  139.         j = get_x_char(15, &inch);
  140.         if (j >= 0 && inch == nak) {
  141.             if (debug) fprintf(errors, "Got a nak after block %d\n",blocknum);
  142.             clear_iq();
  143.             continue;
  144.         }
  145.         if (j >= 0 && inch == can) {
  146.             if (debug) fprintf(errors, "Got a cancel during EOT\n");
  147.             kleenex(0);
  148.         }
  149.         if (j >= 0 && inch != ack) {
  150.             if (debug) fprintf(errors, "Got a non-ack during EOT, char was %o\n",inch);
  151.             clear_iq();
  152.             continue;
  153.         }
  154.         if (j == -1) {
  155.             if (debug) fprintf(errors, "Got a timeout during EOT\n");
  156.             continue;
  157.         }
  158.         break;
  159.     }
  160.     kleenex(0);
  161. }
  162. kleenex(x)
  163. int x;
  164. {
  165.     sleep(1);
  166.     ioctl(1, TCSETA, &save);
  167.     if (debug) fprintf(errors, "Caught signal %d\n", x);
  168.     exit();
  169. }
  170. alarmfn() {
  171.     signal(14, alarmfn);
  172. }
  173. read_in(fp, buf, num)
  174. FILE *fp;
  175. char *buf;
  176. int num;
  177. {
  178.     int i;
  179.     int c;
  180.     static int cr_held = 0;
  181.     static eof_fts = 0;
  182.  
  183.     i = 0;
  184.     if (cr_held) {
  185.         buf[i] = '\n';
  186.         i++;
  187.         cr_held = 0;
  188.     }
  189.     for(   ;i < num;i++) {
  190.         if (eof_fts == 1) return(i);
  191.         c = getc(fp);
  192.         if (c == EOF) {
  193.             eof_fts = 1;
  194.             c = 26;    /* MS-DOS eot Character */
  195.         }
  196.         if (c == '\n' && lfproc) {
  197.             buf[i] = '\r';
  198.             if (i == 127) {
  199.                 cr_held = 1;
  200.                 return(128);
  201.             }
  202.             buf[i+1] = '\n';
  203.             i++;
  204.         }
  205.         else {
  206.             buf[i] = c;
  207.         }
  208.     }
  209.     return(i);
  210. }
  211. clear_iq() {
  212.     int j;
  213.     char ch;
  214.  
  215.     signal(14, alarmfn);
  216.     do {
  217.         alarm(2);
  218.         j = read(0, &ch, 1);
  219.         alarm(0);
  220.         if (debug) fprintf(errors, "cleared line of %o\n", ch);
  221.     } while (j > -1);
  222. }
  223. get_x_char(timeout, cp)
  224. int timeout;
  225. char *cp;
  226. {
  227.     int ch;
  228.     extern int errno;
  229.     int ret;
  230.  
  231.     alarm(timeout);
  232.     if ((ch=getchar()) == EOF) {
  233.         /*if (debug) fprintf(errors, "get_x_char returning read error\n");*/
  234.         /*if (debug) fprintf(errors, "Errno == %d\n", errno);*/
  235.         return(-1);
  236.     }
  237.     alarm(0);
  238.     *cp = ch;
  239.     return( ch );
  240. }
  241.